package com.ruoyi.mushcoin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.mushcoin.domain.bo.TgMessageNoticeBo;
import com.ruoyi.mushcoin.domain.vo.TgMessageNoticeImportVo;
import com.ruoyi.mushcoin.domain.vo.TgMessageNoticeVo;
import com.ruoyi.mushcoin.listener.TgMessageNoticeImportListener;
import com.ruoyi.mushcoin.service.ITgMessageNoticeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * TG消息通知Controller
 *
 * @author billy
 * 2025-03-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/messageNotice")
public class TgMessageNoticeController extends BaseController {
    @Resource
    private ITgMessageNoticeService tgMessageNoticeService;

    /**
     * 查询TG消息通知列表
     */
    @SaCheckPermission("mushcoin:messageNotice:list")
    @GetMapping("/list")
    public TableDataInfo<TgMessageNoticeVo> list(TgMessageNoticeBo tgMessageNoticeBo) {
        return tgMessageNoticeService.selectPage(tgMessageNoticeBo);
    }

    /**
     * 导出TG消息通知列表
     */
    @SaCheckPermission("mushcoin:messageNotice:export")
    @Log(title = "TG消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgMessageNoticeBo tgMessageNoticeBo) {
        List<TgMessageNoticeVo> list = tgMessageNoticeService.selectList(tgMessageNoticeBo);
        ExcelUtil.exportExcel(list, "TG消息通知", TgMessageNoticeVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "TG消息通知", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:messageNotice:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgMessageNoticeImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgMessageNoticeImportVo.class, new TgMessageNoticeImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:messageNotice:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "TG消息通知", TgMessageNoticeImportVo.class, response);
    }

    /**
     * 获取TG消息通知详细信息
     */
    @SaCheckPermission("mushcoin:messageNotice:query")
    @GetMapping(value = "/{id}")
    public R<TgMessageNoticeVo> getInfo(@PathVariable Long id) {
        return R.ok(tgMessageNoticeService.selectById(id));
    }

    /**
     * 新增TG消息通知
     */
    @SaCheckPermission("mushcoin:messageNotice:add")
    @Log(title = "TG消息通知", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgMessageNoticeBo tgMessageNoticeBo) {
        boolean inserted = tgMessageNoticeService.insert(tgMessageNoticeBo);
        if (!inserted) {
            return R.fail("新增TG消息通知记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改TG消息通知
     */
    @SaCheckPermission("mushcoin:messageNotice:edit")
    @Log(title = "TG消息通知", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgMessageNoticeBo tgMessageNoticeBo) {
        Boolean updated = tgMessageNoticeService.update(tgMessageNoticeBo);
        if (!updated) {
            return R.fail("修改TG消息通知记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG消息通知
     */
    @SaCheckPermission("mushcoin:messageNotice:remove")
    @Log(title = "TG消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = tgMessageNoticeService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除TG消息通知记录失败!");
        }
        return R.ok();
    }
}
